﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using System.Collections.Specialized;
using System.Collections;

namespace LuKou.Web.Helper
{
    public static class LinkExtensions
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="tag">是否重设url</param>
        /// <param name="routeValues"></param>
        /// <param name="htmlAttributes"></param>
        /// <returns></returns>
        public static string Link(this HtmlHelper htmlHelper, string linkText, bool tag, bool isSplit, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
        {
            return Link(htmlHelper, linkText, null, null, tag, isSplit, routeValues, htmlAttributes);
        }

        public static string Link(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, bool tag, bool isSplit, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
        {
            if (!string.IsNullOrEmpty(actionName))
            {
                routeValues["action"] = actionName;
            }
            if (!string.IsNullOrEmpty(controllerName))
            {
                routeValues["controller"] = controllerName;
            }
            NameValueCollection strQuery = htmlHelper.ViewContext.RequestContext.HttpContext.Request.QueryString;
            if (tag || strQuery.Count <= 0 || null == routeValues)
            {
                return HtmlHelper.GenerateRouteLink(htmlHelper.ViewContext.RequestContext
                    , htmlHelper.RouteCollection
                    , linkText
                    , null
                    , routeValues
                    , htmlAttributes);
            }

            RouteValueDictionary newRouteValues = routeValues;
            for (int i = 0; i < strQuery.Count; i++)
            {
                string value = strQuery[i];
                string key = strQuery.GetKey(i);
                if (string.IsNullOrEmpty(value)
                    || string.IsNullOrEmpty(key))
                {
                    continue;
                }
                if (!newRouteValues.ContainsKey(key))
                {
                    newRouteValues.Add(key, value);
                    continue;
                }
                if (isSplit)
                {
                    GetSplitRouteValues(newRouteValues, value, key);
                }
            }
            return HtmlHelper.GenerateRouteLink(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection, linkText, null, newRouteValues, htmlAttributes);
        }

        private static void GetSplitRouteValues(RouteValueDictionary newRouteValues, string value, string key)
        {
            Hashtable htValues = new Hashtable();
            Hashtable htNewValues = new Hashtable();
            string[] values = value.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            string[] newValues = newRouteValues[key].ToString().Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            for (int j = 0; j < values.Count(); j++)
            {
                string[] data = values[j].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                htValues.Add(data[0], data[1]);
            }
            for (int k = 0; k < newValues.Count(); k++)
            {
                string[] data = newValues[k].Split(new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                htNewValues.Add(data[0], data[1]);
            }
            foreach (DictionaryEntry data in htNewValues)
            {
                if (!htValues.ContainsKey(data.Key))
                {
                    htValues.Add(data.Key, data.Value);
                    continue;
                }
                htValues[data.Key] = data.Value;
            }
            string strValue = string.Empty;
            foreach (DictionaryEntry data in htValues)
            {
                strValue += string.Format("{0}:{1}|", data.Key, data.Value);
            }
            newRouteValues[key] = strValue.Substring(0, strValue.Length - 1);
        }
    }
}
